/*
 * Remote interface implementation
 */
package examples.rmi;

import java.rmi.*;
import java.rmi.server.*;

/**
 *
 * @author prabhat.totoo
 */
public class ConfusedImpl extends UnicastRemoteObject /* super class that allows us to invoke methods remotely */ 
        implements ConfusedInterface {

    public ConfusedImpl() throws RemoteException {
        super();
    }

    public String processString(String myString) throws RemoteException {

        /* In order to reverse the String, we need a
         * StringBuffer.
         */

        StringBuffer buffer = new StringBuffer(myString);
        buffer = buffer.reverse();

        String reversedString = buffer.toString();

        return reversedString;
    }

    public static void main(String args[]) {

        try {
            /* Instantiate our implementation */
            ConfusedImpl confusedImpl = new ConfusedImpl();

            /* Put it into the rmiregistry.  The first argument
             * is the location of the registry, as well as a name
             * to refer to the object.  We used ‘///’ to indicate
             * that the object will be on the local rmiregistry.
             * The second parameter is the instance that we want to
             * put in the registry
             */
            Naming.rebind("rmi://localhost:1099/confused", confusedImpl);
            System.out.println("Confused object has been bound");

        } catch (Exception e) {

            /* A RemoteException as well as a MalformedURLException
             * can be thrown.  Since we will handle them the same,
             * we just catch the more generic Exception
             */
            System.out.println("An error occurred trying to bind the object to the registry.");
      }
   }
}